/**
	License:		Artistic license 2.0 (see license.txt, also available online at http://www.perlfoundation.org/artistic_license_2_0)
	Authors:		OrbitalLab (http://www.orbitallab.ru/moonglide/), 2007-2009

	File:			application.d
	Description:	application class - starts engine and scenes, rules all
	Date:			30.11.2007 by Digited
**/
module client.Application;

import
	tango.io.Stdout,
	tango.io.Console,
	tango.io.vfs.FileFolder,
	tango.io.vfs.LinkedFolder,
	tango.text.Util,
	tango.sys.Environment,
	tango.util.PathUtil,

	MoonGlide,

	client.customscenes.demoscene.DemoScene;

//================================================================
/// CApplication - main class for custom demos and using of every systems, like engine and network
//================================================================
class CApplication
{
	protected:
		CEngineCore	_engine;

		bool		_configloaded,
					_shutdownDone;

	public:
		//=================================================================
		/// Initialize - setup engine and scene.
		/// Args - program command line input (first comes full application path)
		//=================================================================
		bool initialize( char[][] args )
		{
			debug debugLogB("Application initialization started..." );

			_shutdownDone = false;
			_engine = CEngineCore();

			version( Windows ) args[0] = normalize( args[0] );

			char[] path = normalize( Environment.exePath( args[0] ).toString );
			Cout( path ).newline;

			if( !_engine.Launch( path ))
			{
				errorLog("Could not launch engine!" );
				return false;
			}

			return true;
		}

		//=================================================================
		/// Run - run the engine and scenes and everything cool
		//=================================================================
		void run()
		{
			_engine.run();
		}

		//=================================================================
		/// shutdown - delete all our stuff...
		//=================================================================
		int shutdown()
		{
			if( _shutdownDone )
				return -1;

			debug debugLogB( "Application shutdown started" );

			_shutdownDone = true;

			_engine.shutdown();
			delete _engine;

			debug debugLogB( "Application shutdown ended" );

			return 0;
		}

		this() {}
		~this() { shutdown(); }
}
